/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.itip.collector.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.itip.ProbeEnv;
import com.sinodata.bsm.itip.collector.Collector;
import com.sinodata.bsm.itip.exception.CollectorException;
import com.sinodata.bsm.itip.util.SoapUtil;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-8 上午11:57:08          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class HttpCollector extends Collector {

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        HttpClient httpclient = new DefaultHttpClient();
        String sql = this.paramMap.get("collectCmd");
        String matchNum = this.paramMap.get("matchNum");
        int fieldNum = Integer.parseInt(matchNum);
        String[] propMetas = getPropMeta();
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            HttpPost httppost = new HttpPost(ProbeEnv.tovoliSoap);
            httppost.addHeader("MethodName", "CT_Get");
            httppost.addHeader("InterfaceName", "CT_SOAP");
            httppost.addHeader("MessageType", "Call");
            httppost.addHeader("Content-Type", "text/xml");
            httppost.setEntity(new StringEntity(sql));
            System.out.println("executing request " + httppost.getURI());
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httppost, responseHandler);
            String[] stry = SoapUtil.convertSaopXmlToString(responseBody);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[fieldNum];
            for (int i = 0; i < propValues.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(stry[i], propMetas[i]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return null;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "collectCmd", "matchNum" };
    }

    @Override
    public String[] getPropMeta() {
        int count = Integer.parseInt(this.paramMap.get("matchNum"));
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "$" + i;
        }
        return propMetas;
    }

    public static void main(String[] args) throws Exception {
        new HttpCollector().collect("");
    }

}
